/*

	Test of the Common stuff

	by ELChupathingy

*/

#include "MemoryPool.h"
#include "thread_manager.h"
#include <stdio.h>

#define BOOST_TEST_MODULE "Common Tests"
#include <boost/test/included/unit_test.hpp>


BOOST_AUTO_TEST_CASE( Memory_Pool_Allocation_Test )
{
	printf( "\nMemory_Pool_Allocation_Test\n" );
	MemoryPool<int> p( 2 );
	p.print();
}

BOOST_AUTO_TEST_CASE( Memory_Pool_MemoryPool_Create_Test )
{
	printf( "\nMemory_Pool_MemoryPool_Create_Test\n" );
	MemoryPool<int> p( 2 );
	p.print();

	int *a = p.create();

	*a = 1337;

	printf( "a = %d\n", *a );

	printf( "Before Destroy\n" );

	p.print();

	p.destroy( a );

	a = 0;

	printf( "After Destroyed\n" );

	p.print();
}

BOOST_AUTO_TEST_CASE( Memory_Pool_MemoryPool_Create_Should_Return_Null_When_Max_Reached )
{
	printf( "\nMemory_Pool_MemoryPool_Create_Should_Return_Null_When_Max_Reached\n" );
	MemoryPool<int> p( 2 );
	p.print();

	printf( "Addr: 0x%08X\n", p.create() );
	printf( "Addr: 0x%08X\n", p.create() );
	int *t = p.create();
	BOOST_CHECK_EQUAL( t, (int *)0 );

	p.print();
}

BOOST_AUTO_TEST_CASE( Memory_Pool_Destroy_And_Recreate )
{
	printf( "\nMemory_Pool_Destroy_And_Recreate\n" );

	MemoryPool<int> p( 1 );

	int *a = p.create();

	p.print();

	BOOST_CHECK_EQUAL( p.create(), (int *)0 );

	p.destroy( a );

	a = 0;

	p.print();

	a = p.create();

	BOOST_CHECK( a != (int *)0 );

	p.print();

	a = 0;
}

BOOST_AUTO_TEST_CASE( Memory_Pool_Array_Test )
{
	printf( "\nMemory_Pool_Array_Test\n" );

	MemoryPool<char> p( 1, 64 );

	char *a = p.create();

	p.print();

	strcpy_s( a, 64, "\"cool this worked as I wanted it to\"\n" );

	printf( "\n%s\n", a );

	p.destroy( a );

	a = 0;

	p.print();
}

class job : public thread_job
{
	public:
		job( uint32_t _priv ) : thread_job( this ), priv( _priv ) {}

		void operator ()()
		{
			for( int i = 0; i < 3; i++ )
				printf( "In thread :D %d <- my number\n", priv );
		}

	private:
		uint32_t priv;
};

BOOST_AUTO_TEST_CASE( Thread_manager_short_job_test_creation )
{
	printf( "\nThread_manager_short_job_test_creation\n" );

	job a( 0 ), b( 10 ), c( 5 ), f( 12 );
	//thread_job a1( &a ), b1( &b ), c1( &c ), f1( &f );

	thread_manager t( 2 );
	t.print();

	t.addShortJob( a );
	t.addShortJob( b );
	t.addLongJob( c );
	t.addLongJob( f );

	t.print();
}



